﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.ServiceModel.Web;
using DHQGHN.DAL.BO.TDT;
using DHQGHN.DAL.Models.DanhMuc;
using DHQGHN.DAL.Models.TDT;

namespace DHQGHN.Services.TDT
{
    public class StatisticsService: IStatisticsService
    {
        private StatisticsBO _statisticsBO;
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(StatisticsService));

        public StatisticsService()
        {
            _statisticsBO = new StatisticsBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
        }

        public List<InvestigationStatistics> GetInfo(StatisticsInfoMode info)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<InvestigationStatistics>();
                }
                else
                {
                    info.StartDate = GetDateTime(info.Start_Date);
                    info.EndDate = GetDateTime(info.End_Date);
                    return _statisticsBO.GetInfo(info);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                 throw new FaultException<Fault.UnexpectedServiceFault>(
                   new Fault.UnexpectedServiceFault
                   {
                       ErrorMessage = ex.Message,
                       Source = ex.Source,
                       Target = ex.TargetSite.ToString(),
                       StackTrace = ex.StackTrace
                   },
                   new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public SurveyMethodInfoModel GetInfo()
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new SurveyMethodInfoModel();
                }
                else
                {
                    return _statisticsBO.GetInfo();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                  new Fault.UnexpectedServiceFault
                  {
                      ErrorMessage = ex.Message,
                      Source = ex.Source,
                      Target = ex.TargetSite.ToString(),
                      StackTrace = ex.StackTrace
                  },
                  new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        private DateTime GetDateTime(string date)
        {
            //string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
            //string sysUIFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;

            string[] arrDate = date.Split("/, -".ToCharArray());
            int day = int.Parse(arrDate[0]);
            int month = int.Parse(arrDate[1]);
            int year = int.Parse(arrDate[2]);

            DateTime dt = new DateTime(year, month, day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);

            return dt;
        }
    }
}